# -*- coding: utf-8 -*-

import os
import sys
import re

setenv_bat = '''@ECHO OFF

@REM ---------------------------------
@REM Automatic preparation third-party products if need

IF EXIST %~dp0\prepare.bat (
  START /wait %~dp0\prepare.bat
) ELSE (
  ECHO ERROR! Prepare.bat file is not exist! 
  ECHO May be problems with compilation or/and using SALOME as third-party product.
  echo Please, check existing of %~dp0\prepare.bat and try again!
)
@REM ---------------------------------

@REM -----edit-if-necessary-----------
@REM Architecture (set Win32 or Win64)
@SET ARCH=Win32
@REM ---------------------------------

@REM -----edit-if-necessary-----------
@REM Compile mode (set Release or Debug)
@SET BUILD_MODE=Release
@REM ---------------------------------

@REM ---------------------------------
@REM Getting absolute path for SALOME ROOT DIRECTORY
@cd %~dp0..
@SET SALOME_ROOT_DIR=%cd%
@cd %~dp0
@REM ---------------------------------

@REM -----edit-if-necessary-----------
@REM SALOME PRODUCTS DIRECTORY
IF %BUILD_MODE% == Debug (
  @SET PDIR=%SALOME_ROOT_DIR%\PRODUCTSD
  @SET DESTINATION_DIR=DEBUG
  @rem disable catching of Floating Point Exceptions
  @SET DISABLE_FPE=1
) ELSE (
  @SET PDIR=%SALOME_ROOT_DIR%\PRODUCTS
  @SET DESTINATION_DIR=RELEASE
)
@REM ---------------------------------

@REM ---------------------------------
@REM HOME DIRECTORY
@SET HOME=%SALOME_ROOT_DIR%\TMP
@SET SALOME_TMP_DIR=%SALOME_ROOT_DIR%\TMP
@REM ---------------------------------

@REM ---------------------------------
@REM SALOME DATA DIRECTORY
@SET DATA_DIR=%SALOME_ROOT_DIR%\SAMPLES

@SET list=(LIBBATCH KERNEL GUI GEOM MED SMESH YACS JOBMANAGER PARAVIS HEXABLOCK HEXABLOCKPLUGIN NETGENPLUGIN GHS3DPLUGIN HexoticPLUGIN GHS3DPRLPLUGIN BLSURFPLUGIN ATOMGEN ATOMIC ATOMSOLV PYCALCULATOR CALCULATOR COMPONENT LIGHT PYLIGHT RANDOMIZER SIERPINSKY PYHELLO1 HELLO1)

for %%i in %list% do ( 
  
  SET %%i_ROOT=%SALOME_ROOT_DIR%\MODULES\%%i

  SET %%i_ROOT_DIR=%SALOME_ROOT_DIR%\MODULES\%%i
  if %%i==HEXOTICPLUGIN (
    SET %%i_SRC_DIR=%SALOME_ROOT_DIR%\MODULES\%%i\HexoticPLUGIN_SRC
  ) else (
    SET %%i_SRC_DIR=%SALOME_ROOT_DIR%\MODULES\%%i\%%i_SRC
  )
)

@SET HELLO_ROOT_DIR=%HELLO1_ROOT_DIR%
@SET PYHELLO_ROOT_DIR=%PYHELLO1_ROOT_DIR%
@REM ---------------------------------

SET PATH=%LIBBATCH_ROOT_DIR%\lib;%PATH%

SET VTK_AUTOLOAD_PATH=%GUI_ROOT_DIR%\lib\paraview
@SET PV_PLUGIN_PATH=%PV_PLUGIN_PATH%;%PARAVIS_ROOT_DIR%\lib\paraview

REM ================================
rem Setting PATH, PYTHONPATH for modules.
rem Only for KERNEL and GUI modules environment will be set automatically.
rem If you need to set the environment for some other modules, edit env_m_list.
rem Full env_m_list is (KERNEL GUI GEOM MED SMESH YACS JOBMANAGER PARAVIS HEXABLOCK HEXABLOCKPLUGIN NETGENPLUGIN GHS3DPLUGIN HexoticPLUGIN GHS3DPRLPLUGIN BLSURFPLUGIN ATOMGEN ATOMIC ATOMSOLV PYCALCULATOR CALCULATOR COMPONENT LIGHT PYLIGHT RANDOMIZER SIERPINSKY PYHELLO1 HELLO1)

SET env_m_list=(KERNEL GUI)
FOR %%j in %env_m_list% DO (
  call set_one_module_env.bat %%j
)
REM ================================
'''


    

def ExtractSalome():
    modules_dir = ''
    dest_dir = ''
    modules_src_list={}
    bSrc = True
    bBin = False
    sR = 'RELEASE'
    
    
    if len(sys.argv) < 3:
        path = os.getcwd()
        modules_dir = path
        ret = raw_input("extract source file?(Y/N)")
        if ret.upper() == 'Y':
            bSrc = True
            dest_dir = raw_input('please input source path:\n')  
        elif ret.upper() == 'N':
            bSrc = False
            res = raw_input("extract binary file?(Y/N)")
            if res.upper() == 'Y':
                bBin = True
                dest_dir = raw_input('please input install path:\n')
                pass
            else:
                sys.exit()
        else:
            sys.exit()
    else:
        modules_dir = sys.argv[1]
        dest_dir = sys.argv[2]
    
#    modules = [i for i in os.listdir(modules_dir) if os.path.isdir(modules_dir + '/' + i)]
    if bSrc:
        modules_dir = re.sub(r'/', r'\\',  modules_dir)
        dest_dir = re.sub(r'/', r'\\', dest_dir)
        for i in os.listdir(modules_dir + r"\MODULES"):
            if os.path.isdir(modules_dir + r"\MODULES" + '\\' + i):
                modules_src_list[modules_dir + r"\MODULES" + '\\' + i + '\\' + i + '_SRC'] = dest_dir + '\\' + i + '\\src'
    
        for path in modules_src_list.keys():   
            modules_src_list[path] = re.sub(r'/', r'\\',  modules_src_list[path])
            if not os.path.exists(modules_src_list[path]):
                os.makedirs(modules_src_list[path])
            command = 'xcopy ' + path + '\\src' + ' ' + modules_src_list[path] + ' /S /E'
            print command
            os.system(command)
        os.system("pause")
        pass
    elif bBin:
        
        modules_dir = re.sub(r'/', r'\\',  modules_dir)
        dest_dir = re.sub(r'/', r'\\', dest_dir)
        
        for i in os.listdir(modules_dir + r"\MODULES"):
            if os.path.isdir(modules_dir + r"\MODULES" + '\\' + i):
                modules_src_list[modules_dir + r"\MODULES" + '\\' + i +'\\' + sR + '\\' + i + '_INSTALL'] = \
                dest_dir + r"\MODULES" + '\\' + i
    
        
        for path in modules_src_list.keys():   
            modules_src_list[path] = re.sub(r'/', r'\\',  modules_src_list[path])
            if not os.path.exists(modules_src_list[path]):
                os.makedirs(modules_src_list[path])
            command = 'xcopy ' + path + ' ' + modules_src_list[path]  + ' /S /E'
    #        print command
            os.system(command)
        
        if not os.path.exists(dest_dir + '\\PRODUCTS'):
                os.makedirs(dest_dir + '\\PRODUCTS')
        command = 'xcopy ' + modules_dir + '\\PRODUCTS ' + dest_dir + '\\PRODUCTS /S /E'
        os.system(command)
        
        if not os.path.exists(dest_dir + '\\SAMPLES'):
                os.makedirs(dest_dir + '\\SAMPLES')
        command = 'xcopy ' + modules_dir + '\\SAMPLES ' + dest_dir + '\\SAMPLES /S /E'
        os.system(command)
        
        if not os.path.exists(dest_dir + '\\TMP'):
                os.makedirs(dest_dir + '\\TMP')
        command = 'xcopy ' + modules_dir + '\\TMP ' + dest_dir + '\\TMP /S /E'
        os.system(command)
        
        if not os.path.exists(dest_dir + '\\WORK'):
                os.makedirs(dest_dir + '\\WORK')
        command = 'xcopy ' + modules_dir + '\\WORK ' + dest_dir + '\\WORK /S /E'
        os.system(command)
        
        
        if os.path.exists(dest_dir + '\\WORK\\set_env.bat'):
            if os.path.exists(dest_dir + '\\WORK\\set_env.bat.bak'):
                os.remove(dest_dir + '\\WORK\\set_env.bat.bak')
            os.rename(dest_dir + '\\WORK\\set_env.bat', dest_dir + '\\WORK\\set_env.bat.bak')
        
        f = open(dest_dir + '\\WORK\\set_env.bat', 'w')
        f.write(setenv_bat)
        f.close()
        
        os.system("pause")
        pass
    else:
        sys.exit()
    
    
    
    
    
    
if __name__ == '__main__':
    ExtractSalome()
